package com.itheima03.day12Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author 刘全
 * @date 2021/8/4
 */
public class Test3 {
    public static void main(String[] args) throws IOException {
        //尝试用代码完成文件夹的复制（题目有点难，注意：不能直接使用IO流复制文件夹，IO流只能复制文件）
        File dir = new File("C:\\Users\\lq\\Desktop\\Genshin-Wish-Export");
        File src = new File("test\\");
        copyDir(dir, src);
    }

    private static void copyDir(File dir, File src) throws IOException {
        src = new File(src, dir.getName());
        if (!src.exists()) {
            src.mkdirs();
        }
        File[] files = dir.listFiles();
        if (files != null) {
            for (File f : files) {
                if (f.isFile()) {
                    FileInputStream fis = new FileInputStream(f);
                    FileOutputStream fos = new FileOutputStream(new File(src, f.getName()));
                    byte[] bytes = new byte[8192];
                    int len;
                    while ((len = fis.read(bytes)) != -1) {
                        fos.write(bytes, 0, len);
                    }
                    fis.close();
                    fos.close();
                } else {
                    copyDir(f,src);
                }
            }
        }
    }
}
